package com.tao.config.shiro;

import com.tao.config.ApplicationContextUtil;
import org.apache.catalina.core.ApplicationContext;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Set;
@Component

public class RedisCache<k,v> implements Cache<k,v> {

    //为了  把cacheName传进来
    private    String cacheName;

    public RedisCache(String cacheName) {
        this.cacheName = cacheName;
    }
    public RedisCache(){}


    @Override
    public v get(k k) throws CacheException {
//        System.out.println("RedisCache getKey:"+k);
//        return      (v)getRedisTemplate().opsForValue().get(k.toString());


        System.out.println("CacheName:"+this.cacheName);
        //hash
                System.out.println("RedisCache getKey:"+k);

        return      (v)getRedisTemplate().opsForHash().get(this.cacheName,k.toString());

    }

    /*
    输出:
    RedisCache Key:root
    RedisCache Value:User(id=12, username=root, password=123, perms=root, tags=null, head=head3, plan=4)
    测试后能进入！！
     */
    @Override
    public v put(k k, v v) throws CacheException {
        System.out.println("RedisCache Key:"+k);
        System.out.println("RedisCache Value:"+v);

//        //key value 注入
//        RedisTemplate redisTemplate = getRedisTemplate();
//        redisTemplate.opsForValue().set(k.toString(),v);

        //键值对 hash模式注入
        //大key cacheName
        System.out.println("cacheName:"+cacheName);
        getRedisTemplate().opsForHash().put(this.cacheName,k.toString(),v);
        return null;
    }

    @Override
    public v remove(k k) throws CacheException {
        System.out.println(this.cacheName+"======remove========="+k);
        System.out.println((v) getRedisTemplate().opsForHash().get(this.cacheName,k.toString()));
        return (v)getRedisTemplate().opsForHash().delete(this.cacheName,k.toString());
    }

    @Override
    public void clear() throws CacheException {
        System.out.println("======clear=========");

        getRedisTemplate().delete(this.cacheName);
    }

    @Override
    public int size() {
        return getRedisTemplate().opsForHash().size(this.cacheName).intValue();
    }

    @Override
    public Set<k> keys() {
        return getRedisTemplate().opsForHash().keys(this.cacheName);
    }

    @Override
    public Collection<v> values() {
        return  getRedisTemplate().opsForHash().values(this.cacheName);
    }

    private RedisTemplate getRedisTemplate(){
        //因为没有在容器中，所以手动配置
        RedisTemplate redisTemplate = (RedisTemplate)ApplicationContextUtil.getBean("redisTemplate");
        redisTemplate.setKeySerializer(new StringRedisSerializer());//key 设置序列化方式
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());//设置Hash的
        return redisTemplate;
    }
}
